package com.mini.auth.config;

import com.mini.auth.granter.SmsTokenGranter;
import com.mini.auth.service.ClientDetailsServiceImpl;
import com.mini.auth.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.CompositeTokenGranter;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.TokenGranter;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter;
import org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter;
import org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.util.ArrayList;

/**
 * 认证授权服务器配置
 * @author Zhou Feng
 */
@EnableAuthorizationServer
@Configuration
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    @Qualifier("jwtStore")
    private JwtTokenStore jwtTokenStore;
    @Autowired
    private TokenEnhancer tokenEnhancer;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsServiceImpl userDetailsService;
    @Autowired
    private ClientDetailsServiceImpl clientDetailsService;


    //用来配置客户端的相关信息
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //配置client
        clients.withClientDetails(clientDetailsService);
    }

    //用来配置授权（authorization）以及令牌（token）的访问端点和令牌服务(token services)
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
                //refresh_token模式需要 userDetailsService
                .userDetailsService(userDetailsService)
                .tokenStore(jwtTokenStore)
                .tokenEnhancer(tokenEnhancer);
        //配置自定义授权方式，可根据需要加添Spring Security自带的授权方式
        ArrayList<TokenGranter> tokenGranters = new ArrayList<>();
        AuthorizationServerTokenServices tokenServices = endpoints.getTokenServices();
        //存储授权码服务，默认授权码存在内存中
        AuthorizationCodeServices authorizationCodeServices = endpoints.getAuthorizationCodeServices();
        OAuth2RequestFactory requestFactory = endpoints.getOAuth2RequestFactory();
        //自带的授权码模式
        tokenGranters.add(new AuthorizationCodeTokenGranter(tokenServices, authorizationCodeServices,
                clientDetailsService, requestFactory));
        //启用 refresh_token
        tokenGranters.add(new RefreshTokenGranter(tokenServices, clientDetailsService, requestFactory));
        //自带的password模式，添加 authenticationManager
        tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices,
                clientDetailsService, requestFactory));
        //自定义的短信登录模式
        tokenGranters.add(new SmsTokenGranter(authenticationManager, tokenServices,
                clientDetailsService, requestFactory));
        //使用这些授权逻辑
        endpoints.tokenGranter(new CompositeTokenGranter(tokenGranters));
    }

    //用来配置令牌端点(Token Endpoint)的安全约束
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //security.allowFormAuthenticationForClients();
    }
}
